Plotting in R

Maria Pachiadaki

11/16/2021

Explore different types of plots in ggplot2

ggplot2 BLURB

Required packages

Install the following packages: - palmerpenguin - tidyvers - ggpubr

Dataset

We will use the palmerpenguins dataset. Data were collected and made available by Dr. Kristen Gorman and the Palmer Station, Antarctica LTER, a member of the Long Term Ecological Research Network.

We will briefly check the datastructure before we start plotting.

library(palmerpenguins)  # load palmerpenguins dataset
head(penguins)  #check table structure
# A tibble: 6 x 8
  species island bill_length_mm bill_depth_mm flipper_length_… body_mass_g sex  
  <fct>   <fct>           <dbl>         <dbl>            <int>       <int> <fct>
1 Adelie  Torge…           39.1          18.7              181        3750 male 
2 Adelie  Torge…           39.5          17.4              186        3800 fema…
3 Adelie  Torge…           40.3          18                195        3250 fema…
4 Adelie  Torge…           NA            NA                 NA          NA <NA> 
5 Adelie  Torge…           36.7          19.3              193        3450 fema…
6 Adelie  Torge…           39.3          20.6              190        3650 male 
# … with 1 more variable: year <int>
summary(penguins)  #summarize data
      species          island    bill_length_mm  bill_depth_mm  
 Adelie   :152   Biscoe   :168   Min.   :32.10   Min.   :13.10  
 Chinstrap: 68   Dream    :124   1st Qu.:39.23   1st Qu.:15.60  
 Gentoo   :124   Torgersen: 52   Median :44.45   Median :17.30  
                                 Mean   :43.92   Mean   :17.15  
                                 3rd Qu.:48.50   3rd Qu.:18.70  
                                 Max.   :59.60   Max.   :21.50  
                                 NA's   :2       NA's   :2      
 flipper_length_mm  body_mass_g       sex           year     
 Min.   :172.0     Min.   :2700   female:165   Min.   :2007  
 1st Qu.:190.0     1st Qu.:3550   male  :168   1st Qu.:2007  
 Median :197.0     Median :4050   NA's  : 11   Median :2008  
 Mean   :200.9     Mean   :4202                Mean   :2008  
 3rd Qu.:213.0     3rd Qu.:4750                3rd Qu.:2009  
 Max.   :231.0     Max.   :6300                Max.   :2009  
 NA's   :2         NA's   :2                                 

As we can see from the summary table three different species of penguins were recorded in three different islands.

Scatterplot

Let’s explore if there is a correlation between the body mass of the penguins and the flipper length

library(tidyverse)  # load the tidyverse package; contains ggplot2

ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point()

Let’s add a trendline

ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point() + geom_smooth(method = "lm")

Let’s add a trendline together with the equation

library(ggpubr)  #package the facilitates the display of the equation

ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g)) + geom_point() + geom_smooth(method = "lm") +
    stat_regline_equation(label.y = 6000, aes(label = ..eq.label..)) + stat_regline_equation(label.y = 5600,
    aes(label = ..rr.label..))

Are there any differences between the species?

# regression equations will overlap, we will use faceting for them
ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
    geom_point() + geom_smooth(method = "lm")

ggplot(penguins, aes(x = flipper_length_mm, y = body_mass_g, color = species)) +
    geom_point(aes(shape = sex))

Themes

There are built in ggplot themes or there is a long list of cosmetic changes you can make with theme(). Let’s try changing themes in other type of plot, histograms. Let’s plot the distribution of the flipper length for each species. We will use my favourite theme: them_bw()

https://r-charts.com/ggplot2/themes/

ggplot(penguins, aes(flipper_length_mm, fill = species)) + geom_histogram(alpha = 0.6,
    position = "identity") + theme_bw()

ggplot(penguins, aes(flipper_length_mm, fill = species)) + geom_histogram(alpha = 0.6,
    position = "identity") + theme_void()

Themes can be modified

Labels

ggplot(penguins, aes(flipper_length_mm, fill = species)) + geom_histogram(alpha = 0.6,
    position = "identity") + theme_bw() + labs(x = "Flipper length (mm)", y = "Counts") +
    theme(axis.title.x = element_text(color = "black", face = "bold", size = 14),
        axis.title.y = element_text(color = "black", face = "bold", size = 14))

Axis

# modify axis font size
ggplot(penguins, aes(flipper_length_mm, fill = species)) + geom_histogram(alpha = 0.6,
    position = "identity") + theme_bw() + labs(x = "Flipper length (mm)", y = "Counts") +
    theme(axis.text.x = element_text(color = "black", size = 12), axis.text.y = element_text(color = "black",
        size = 12), axis.title.x = element_text(color = "black", face = "bold", size = 14),
        axis.title.y = element_text(color = "black", face = "bold", size = 14))

You can also change the color, angle, and justification of the axis labels.

# modify axis font size
ggplot(penguins, aes(flipper_length_mm, fill = species)) + geom_histogram(alpha = 0.6,
    position = "identity") + theme_bw() + labs(x = "Flipper length (mm)", y = "Counts") +
    theme(axis.text.x = element_text(color = "black", size = 12, angle = 45), axis.text.y = element_text(color = "black",
        size = 12), axis.title.x = element_text(color = "grey30", face = "bold",
        size = 14), axis.title.y = element_text(color = "grey30", face = "bold",
        size = 14))

The horizontal or vertical justification, (hjust and vjust) can also be adjusted. This hjust and vjust argument can be best explained using this figure [Source from Stackoverflow]:

e.g.:

ggplot(penguins, aes(flipper_length_mm, fill = species)) + geom_histogram(alpha = 0.6,
    position = "identity") + theme_bw() + labs(x = "Flipper length (mm)", y = "Counts") +
    theme(axis.text.x = element_text(color = "black", size = 12, angle = 45, hjust = 1,
        vjust = 1), axis.text.y = element_text(color = "black", size = 12), axis.title.x = element_text(color = "grey30",
        face = "bold", size = 14), axis.title.y = element_text(color = "grey30",
        face = "bold", size = 14))

Legends

ggplot(penguins, aes(flipper_length_mm, fill = species)) + geom_histogram(alpha = 0.6,
    position = "identity") + theme_bw() + labs(x = "Flipper length (mm)", y = "Counts",
    fill = "Species") + theme(axis.text.x = element_text(color = "black", size = 12),
    axis.text.y = element_text(color = "black", size = 12), axis.title.x = element_text(color = "black",
        face = "bold", size = 14), axis.title.y = element_text(color = "black", face = "bold",
        size = 14), legend.title = element_text(color = "black", face = "bold", size = 14),
    legend.text = element_text(size = 12))

###Colors

Barplot

Boxplots

ggplot(penguins, aes(x = species, y = flipper_length_mm, fill = sex)) + geom_boxplot() +
    theme_bw() + labs(x = "Species", y = "Flipper length (mm)") + theme(axis.text.x = element_text(color = "black",
    size = 12), axis.text.y = element_text(color = "black", size = 12), axis.title.x = element_text(color = "black",
    face = "bold", size = 14), axis.title.y = element_text(color = "black", face = "bold",
    size = 14))

ggplot(na.omit(penguins), aes(x = species, y = flipper_length_mm, fill = sex)) +
    geom_boxplot() + theme_bw() + labs(x = "Species", y = "Flipper length (mm)") +
    theme(axis.text.x = element_text(color = "black", size = 12), axis.text.y = element_text(color = "black",
        size = 12), axis.title.x = element_text(color = "black", face = "bold", size = 14),
        axis.title.y = element_text(color = "black", face = "bold", size = 14))

ggplot(na.omit(penguins), aes(x = species, y = flipper_length_mm, fill = sex)) +
    geom_boxplot() + geom_jitter(color = "black", size = 0.4, alpha = 0.9) + theme_bw() +
    labs(x = "Species", y = "Flipper length (mm)") + theme(axis.text.x = element_text(color = "black",
    size = 12), axis.text.y = element_text(color = "black", size = 12), axis.title.x = element_text(color = "black",
    face = "bold", size = 14), axis.title.y = element_text(color = "black", face = "bold",
    size = 14))

Violinplots

ggplot(na.omit(penguins), aes(x = species, y = flipper_length_mm, fill = sex)) +
    geom_violin() + theme_bw() + labs(x = "Species", y = "Flipper length (mm)") +
    theme(axis.text.x = element_text(color = "black", size = 12), axis.text.y = element_text(color = "black",
        size = 12), axis.title.x = element_text(color = "black", face = "bold", size = 14),
        axis.title.y = element_text(color = "black", face = "bold", size = 14))

ggplot(na.omit(penguins), aes(x = species, y = flipper_length_mm, fill = sex)) +
    geom_violin() + geom_boxplot(position = position_dodge(width = 0.9), width = 0.2) +
    theme_bw() + labs(x = "Species", y = "Flipper length (mm)") + theme(axis.text.x = element_text(color = "black",
    size = 12), axis.text.y = element_text(color = "black", size = 12), axis.title.x = element_text(color = "black",
    face = "bold", size = 14), axis.title.y = element_text(color = "black", face = "bold",
    size = 14))

Faceting

Colors

Plot interactively with plotly or dygraphs

library(plotly)